﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace SwormTests.IUD
{
    public class without_auto_increment : TestBase
    {
        public without_auto_increment(DbToTest type) : base(type) { }

        class simple
        {
            [Sworm.Column(IsPrimaryKey = true, IsAutoIncrement = false)]
            public long id { get; set; }
            public string string1 { get; set; }
            public string string2 { get; set; }
        }

        [Test]
        public void can_update()
        {
            var item1 = new simple() { id = 1, string1 = "item1", string2 = "asdf" };
            var item2 = new simple() { id = 2, string1 = "item2", string2 = "qwer" };
            DB.Insert(item1);
            DB.Insert(item2);

            item1 = DB.Query.Joining<simple>("c")
                .Where(@"c.id = @0", 1)
                .List().Single();

            Assert.AreEqual("item1", item1.string1);
            Assert.AreEqual("asdf", item1.string2);

            item1.string1 = "hello";
            item1.string2 = null;
            DB.Update(item1);

            item1 = DB.Query.Joining<simple>("c")
                .Where(@"c.id = @0", 1)
                .List().Single();

            Assert.AreEqual(1, item1.id);
            Assert.AreEqual("hello", item1.string1);
            Assert.IsNull(item1.string2);

            item1 = DB.Query.Joining<simple>("c")
                .Where(@"c.id = @0", 2)
                .List().Single();

            Assert.AreEqual("item2", item2.string1);
            Assert.AreEqual("qwer", item2.string2);
        }

        [Test]
        public void can_delete()
        {
            int rowsAffected = DB.Insert(new simple() { id = 3 });
            Assert.AreEqual(1, rowsAffected);

            rowsAffected = DB.Delete(new simple() { id = 1904 });
            Assert.AreEqual(0, rowsAffected);

            rowsAffected = DB.Delete(new simple() { id = 3 });
            Assert.AreEqual(1, rowsAffected);
        }
    }
}
